from Crypto.Cipher import AES
from base64 import b64encode
import requests
import json


url = 'https://music.163.com/weapi/comment/resource/comments/get?csrf_token='
data = {
    "csrf_token": "",
    "id": "573968836",
    "lv": "-1",
    "tv": "-1"
}

e = '010001'
f = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
g = '0CoJUm6Qyw8W8jud'
i = '"vBPc6tvKQdG7fYEW"'  # 手动固定的 ——> 人家函数中为随机

def get_encSecKey():
    return "946846656d3aa87166ccb8737f3278b66a41f798a58e6b780f3fef55eb1fe777cbddecd9a7beff555ce44145a939ffb0dfc22853ae68a51074223a7a08c7883a5c61fae691d819b7bb066d7694dffa29544b403cc60e83c896fd84207b0840dd1a9401f2cd978a3cac4aff6458c5e22bce4109a64c03575937697f21d5aa0977"

# 把参数进行加密
def get_params(data):  # 默认收到的是字符串
    first = enc_params(data,g)  # 第一次加密
    second = enc_params(first, i)  # 第二次加密
    return second # 返回为params

# 转化成16的倍数，为下方的加密算法服务
def to_16(data):
    pad = 16 - (len(data) % 16)
    data += chr(pad) * pad
    return data

# 加密过程
def enc_params(data,key):  # 加密过程
    iv = '0102030405060708'
    data = to_16(data)
    aes = AES.new(key = key.encode('utf-8'),IV=iv.encode('utf-8'),mode=AES.MODE_CBC)  # 创建加密器
    bs = aes.encrypt(data.encode('utf-8'))  #加密 加密的长度必须是16的倍数
    return str(b64encode(bs),'utf-8')  # 转换成字符串返回

# 处理加密过程
'''
function a(a=16) {  随机的16位字符串
        var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c = "";
        for (d = 0; a > d; d += 1)  循环16次
            e = Math.random() * b.length,  随机数
            e = Math.floor(e),  取整
            c += b.charAt(e); 取字符串中的xxx位置
        return c
    }
    function b(a, b) {
        var c = CryptoJS.enc.Utf8.parse(b)
          , d = CryptoJS.enc.Utf8.parse("0102030405060708")
          , e = CryptoJS.enc.Utf8.parse(a)
          , f = CryptoJS.AES.encrypt(e, c, {  # AES加密
            iv: d,  # 偏移量
            mode: CryptoJS.mode.CBC
        });
        return f.toString()
    }
    function c(a, b, c) {
        var d, e;
        return setMaxDigits(131),
        d = new RSAKeyPair(b,"",c),
        e = encryptedString(d, a)
    }
    function d(d, e, f, g) {
        var h = {}  空对象
          , i = a(16);   i为一个16位的随机值
        return h.encText = b(d, g),
        h.encText = b(h.encText, i),  返回的为params
        h.encSecKey = c(i, e, f),  返回的为encSecKey
        h
    }
'''

'''
d:data(数据) e:010001 
'''

res = requests.post(url,data={
    'params':get_params(json.dumps(data)),  # 把字典转化为json的字符串
    'encSecKey':get_encSecKey()
})

print(res.text)



